package cn.pku.eecs.ldn.activity;

import java.util.ArrayList;
import java.util.Calendar;

import cn.pku.eecs.ldn.R;
import cn.pku.eecs.ldn.view.listview.PullUpAndDownListView;
import cn.pku.eecs.ldn.view.listview.PullUpAndDownListView.OnPullListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ArrayAdapter;

public class TestPullListViewActivity extends Activity {

	PullUpAndDownListView listView = null;
	ArrayAdapter<String> adapter;
	private Handler mHandler;

	ArrayList<String> items = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_pull_listview);
		mHandler = new Handler();
		geneItems();
		listView = (PullUpAndDownListView) findViewById(R.id.pullUpAndDownListView);
		adapter = new ArrayAdapter<String>(this, R.layout.list_item, items);
		listView.setAdapter(adapter);
		listView.setPullDownRefreshingEnabled(true);
		Calendar calendar = Calendar.getInstance();
		calendar.set(2011, 1, 5);
		listView.setLastUpdatedTime(calendar.getTimeInMillis());
		listView.setOnPullListener(new OnPullListener() {
			
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
//				Thread t = new Thread(new Runnable() {
//					
//					@Override
//					public void run() {
//						// TODO Auto-generated method stub
//						try {
//							Thread.sleep(2000);
//						} catch (InterruptedException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//						items.clear();
//						geneItems();
//						adapter.notifyDataSetChanged();
//						Log.i("LDN", "onRefresh(): done");
//						listView.stopRefreshing();
//					}
//				});
//				t.run();
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						items.clear();
						geneItems();
						adapter.notifyDataSetChanged();
						Log.i("LDN", "onRefresh(): done");
						listView.stopRefreshing();
					}
				}, 5000);
			}
			
			@Override
			public void onLoadMore() {
				// TODO Auto-generated method stub
				
			}
		});
	}

	int count = 0;
	private void geneItems() {
		for (int i = 0; i < 100; ++i) {
			items.add("refresh cnt " + count++);
		}
	}

}
